/**
 * 
 * @author Travis C lynch <travisclynch@student.midlandstech.edu>
 * @version Mar 22 2012
 * <p>
 * <b>Instructions:</b><br>
 * Write a class with an internal iterator that can be used to print out the Fibonacci sequence, starting from Fibonacci number 0, which is 0 and Fibonacci number 1, which is 1, then each next number being the sum of the previous two (#2 is 1, #3 is 2, #4 is 3, #5 is 5, etc.). 
 * The constructor for your class should have an int parameter that specifies the maximum Fibonacci number. In your inner class, the remove() method should just throw an UnsupportedOperationException.
 * Test your class with a driver program that will prompt for and input an integer, instantiate a Fibonacci object with that integer as the maximum Fibonacci number, and print out all of the Fibonacci numbers up to that number, using an enhanced for loop.
 * Upload an .zip archive of your well-documented Java source code in JavaDoc style to this dropbox.
 * <p>
 * <b>Description:</b><br>
 * A class that uses generic data types
 * 
 */
import java.util.*;
public class Fibonacci implements Iterable<Integer>  {
	/** total number of Fib numbers to calculate*/
	private int max;
	/** previous number */
	private int previous = 0;
	/** current number */
	private int current = 1;
	/** @param a number of times to generate fib number. Accessor method for max */
	public Fibonacci (int a){
		max = a;		
	}

	public Iterator<Integer> iterator() {
		return new SubClass();
	}
	
	private class SubClass implements Iterator<Integer> {
		/** keeps track of how many fib numbers was generated */
		int count = 0;
		/** @return Returns the next fib number in sequence */
		public Integer next() {
			Integer result;
			result = previous;
			if (count == 1) result = current;
			else {
				result = current + previous;
				previous = current;
				current = result;
			}
			count++;
			return result;
		}
		/** @return return true if we are within the max number */
		public boolean hasNext() {
			return count<max;
		}
		public void remove() {
				throw new UnsupportedOperationException();
		}

	}

	public static void main(String[] args) {
		int size;
		Scanner kbd = new Scanner(System.in);
		System.out.print("Please enter how many Fib nubers to calculate: ");
		size=kbd.nextInt();
		Fibonacci fib = new Fibonacci(size); 
		for (int num : fib) System.out.print(num + " ");
	}

}
